package com.cyt.wx.service.impl;

import cn.hutool.core.util.StrUtil;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.BaseConstant;
import com.cyt.base.util.BaseUtil;
import com.cyt.base.util.ErrorCode;
import com.cyt.sys.model.po.SysUser;
import com.cyt.sys.service.impl.BaseServiceImpl;
import com.cyt.wx.dao.WxApplyUseCategoryDao;
import com.cyt.wx.model.po.WxApplyUseCategory;
import com.cyt.wx.model.vo.WxApplyUseCategoryVo;
import com.cyt.wx.service.WxApplyUseCategoryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 微信应用分类关联-微信应用分类关联Service层实现类
 *
 * @author congcong tao
 * @date 2019-8-14 10:40:36
 */
@Service("wxApplyUseCategoryService")
public class WxApplyUseCategoryServiceImpl extends BaseServiceImpl<WxApplyUseCategory> implements WxApplyUseCategoryService {

    @Resource
    private WxApplyUseCategoryDao wxApplyUseCategoryDao;

    @Autowired
    public void setBaseDao(WxApplyUseCategoryDao wxApplyUseCategoryDao) {
        this.baseDao = wxApplyUseCategoryDao;
    }

    /**
     * 分页查询
     *
     * @param wxApplyUseCategoryVo 接受参数对象
     * @param pageNum 分页数
     * @param pageSize 每页显示条数
     * @return ReturnResult
     * @author congcong tao
     * @date 2019-8-14 10:40:36
     */
    @Override
    public ReturnResult findPage(WxApplyUseCategoryVo wxApplyUseCategoryVo, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(" create_time desc");
        Example example = new Example(WxApplyUseCategory.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("removeTag", BaseConstant.REMOVE_TAG_NO);
        List<WxApplyUseCategory> list = wxApplyUseCategoryDao.selectByExample(example);
        PageInfo<WxApplyUseCategory> page = new PageInfo<>(list);
        return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * 分页查询
     *
     * @param wxApplyUseCategoryVo 接受参数对象
     * @param pageNum 分页数
     * @param pageSize 每页显示条数
     * @return ReturnResult
     * @author congcong tao
     * @date 2019-8-14 10:40:36
     */
    @Override
    public ReturnResult findVoPage(WxApplyUseCategoryVo wxApplyUseCategoryVo, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(" create_time desc");
        List<WxApplyUseCategoryVo> list = wxApplyUseCategoryDao.selectVoList(wxApplyUseCategoryVo);
        PageInfo<WxApplyUseCategoryVo> page = new PageInfo<>(list);
        return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * list查询
     *
     * @param wxApplyUseCategoryVo 接受参数对象
     * @return List<WxApplyUseCategory>
     * @author congcong tao
     * @date 2019-8-14 10:40:36
     */
    @Override
    public List<WxApplyUseCategory> findList(WxApplyUseCategoryVo wxApplyUseCategoryVo) {
        Example example = new Example(WxApplyUseCategory.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotEmpty(wxApplyUseCategoryVo.getApplyUseId())) {
            criteria.andEqualTo("applyUseId", wxApplyUseCategoryVo.getApplyUseId());
        }
        criteria.andEqualTo("removeTag", BaseConstant.REMOVE_TAG_NO);
        return wxApplyUseCategoryDao.selectByExample(example);
    }

    /**
     * list查询
     *
     * @param wxApplyUseCategoryVo 接受参数对象
     * @return List<WxApplyUseCategory>
     * @author congcong tao
     * @date 2019-8-14 10:40:36
     */
    @Override
    public List<WxApplyUseCategoryVo> findVoList(WxApplyUseCategoryVo wxApplyUseCategoryVo) {
        return wxApplyUseCategoryDao.selectVoList(wxApplyUseCategoryVo);
    }

    /**
     * 单个对象查询
     *
     * @param wxApplyUseCategoryVo 接受参数对象
     * @return WxApplyUseCategory
     * @author congcong tao
     * @date 2019-8-14 10:40:36
     */
    @Override
    public WxApplyUseCategory findOne(WxApplyUseCategoryVo wxApplyUseCategoryVo) {
        WxApplyUseCategory wxApplyUseCategory = new WxApplyUseCategory();
        if (StrUtil.isEmpty(wxApplyUseCategoryVo.getId())) {
            wxApplyUseCategory.setId(wxApplyUseCategoryVo.getId());
        }
        return wxApplyUseCategoryDao.selectOne(wxApplyUseCategory);
    }

    /**
     * 单个对象查询
     *
     * @param wxApplyUseCategoryVo 接受参数对象
     * @return WxApplyUseCategory
     * @author congcong tao
     * @date 2019-8-14 10:40:36
     */
    @Override
    public WxApplyUseCategoryVo findVoOne(WxApplyUseCategoryVo wxApplyUseCategoryVo) {
        return wxApplyUseCategoryDao.selectVoOne(wxApplyUseCategoryVo);
    }

    /**
     * 添加或更新
     *
     * @param wxApplyUseCategoryVo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author congcong tao
     * @date 2019-8-14 10:40:36
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult addOrUpdate(WxApplyUseCategoryVo wxApplyUseCategoryVo, SysUser sysUser) {
        WxApplyUseCategory wxApplyUseCategory;
        //初始化对象
        if (StrUtil.isEmpty(wxApplyUseCategoryVo.getId())) {
            wxApplyUseCategory = new WxApplyUseCategory(sysUser);
        } else {
            wxApplyUseCategory = wxApplyUseCategoryDao.selectByPrimaryKey(wxApplyUseCategoryVo.getId());
        }
        //设置属性值
        wxApplyUseCategory.setApplyUseId(wxApplyUseCategoryVo.getApplyUseId());
        wxApplyUseCategory.setCategoryId(wxApplyUseCategoryVo.getCategoryId());
        //提交数据库
        if (StrUtil.isEmpty(wxApplyUseCategoryVo.getId())) {
            wxApplyUseCategoryDao.insert(wxApplyUseCategory);
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_INSERT);
        } else {
            wxApplyUseCategoryDao.updateByPrimaryKey(wxApplyUseCategory);
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_UPDATE);
        }
    }

    /**
     * 根据条件逻辑删除
     *
     * @param wxApplyUseCategoryVo 参数接受对象
     * @param sysUser 当前用户
     * @return ReturnResult
     * @author congcong tao
     * @date 2019-8-14 10:40:36
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult remove(WxApplyUseCategoryVo wxApplyUseCategoryVo, SysUser sysUser) {
        //修改字段
        WxApplyUseCategory wxApplyUseCategory = new WxApplyUseCategory();
        wxApplyUseCategory.setRemoveTag(BaseConstant.REMOVE_TAG_YES);
        //筛选条件
        Example example = new Example(WxApplyUseCategory.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(wxApplyUseCategoryVo.getId())) {
            criteria.andIn("id", Arrays.asList(wxApplyUseCategoryVo.getId().split(",")));
        }
        criteria.andEqualTo("removeTag", BaseConstant.REMOVE_TAG_NO);
        wxApplyUseCategoryDao.updateByExampleSelective(wxApplyUseCategory, example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    /**
     * 根据条件物理删除
     *
     * @param wxApplyUseCategoryVo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author congcong tao
     * @date 2019-8-14 10:40:36
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult delete(WxApplyUseCategoryVo wxApplyUseCategoryVo, SysUser sysUser) {
        //筛选条件
        Example example = new Example(WxApplyUseCategory.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(wxApplyUseCategoryVo.getId())) {
            criteria.andIn("id", Arrays.asList(wxApplyUseCategoryVo.getId().split(",")));
        }
        if (StringUtils.isNotBlank(wxApplyUseCategoryVo.getApplyUseId())) {
            criteria.andEqualTo("applyUseId", wxApplyUseCategoryVo.getApplyUseId());
        }
        wxApplyUseCategoryDao.deleteByExample(example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    @Override
    public ReturnResult batchSave(String applyUseId, String categoryIds, SysUser sysUser) {
        WxApplyUseCategoryVo wxApplyUseCategoryVo = new WxApplyUseCategoryVo();
        wxApplyUseCategoryVo.setApplyUseId(applyUseId);
        delete(wxApplyUseCategoryVo, sysUser);

        if (StringUtils.isEmpty(categoryIds)) {
            new ReturnResult(ErrorCode.CODE_1, BaseConstant.DATABASE_ERROR_INSERT);
        }
        String[] categoryIdArr = categoryIds.split(",");
        for (String categoryId : categoryIdArr) {
            WxApplyUseCategory wxApplyUseCategory = new WxApplyUseCategory();
            wxApplyUseCategory.setId(BaseUtil.uId());
            wxApplyUseCategory.setRemoveTag(BaseConstant.REMOVE_TAG_NO);
            wxApplyUseCategory.setApplyUseId(applyUseId);
            wxApplyUseCategory.setCategoryId(categoryId);
            this.insert(wxApplyUseCategory);
        }
        return new ReturnResult(ErrorCode.CODE_0, ErrorCode.CODE_0_MSG);
    }
}